//
//  LoginViewController.swift
//  kingWeiBo
//
//  Created by King on 15/9/7.
//  Copyright © 2015年 King. All rights reserved.
//

import UIKit

protocol LoginViewControllerDelegate: NSObjectProtocol{
    /// 将要登录
    func loginViewWillRegistr()
    /// 将要注册
    func loginViewWillLogin()
}

class LoginViewController: UIView {
    
    
    // 设置代理-----这里需要用weak
    weak var delegate: LoginViewControllerDelegate?
    func login(){
        delegate?.loginViewWillLogin()
    }
    func regin(){
        delegate?.loginViewWillRegistr()
    }
    
    /// 设置每个页面的视图
    func setUpViewInfo(isHome:Bool, message: String, image:String ) {
        homeIconViwe.hidden = !isHome
        textLable.text = message
        iconView.image = UIImage(named: image)
        if !isHome{
            sendSubviewToBack(masksView)
        }else{
            setpAnimation()
        }
    }
    
    /// 设置首页动画
    
    func setpAnimation (){
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = M_PI * 2
        ani.repeatCount = MAXFLOAT
        ani.duration = 20.0
        // 退出桌面后再进入依旧动画的设置
        ani.removedOnCompletion = false
        iconView.layer.addAnimation(ani, forKey: nil)
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setNotLoginView()

    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setNotLoginView()
    }


    //MARK: - 设置未登录界面home界面的view
    func setNotLoginView() {

        addSubview(iconView)
        addSubview(masksView)
        addSubview(homeIconViwe)
        addSubview(textLable)
        addSubview(reginButton)
        addSubview(loginButton)
    
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //给小房子设置约束
        homeIconViwe.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconViwe, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconViwe, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //给信息lable设置约束
        textLable.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.CenterX , relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        
            ///注册按钮设置约束

        reginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: reginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: reginButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: reginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: reginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        ///登录按钮设置约束
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        /// 遮罩约束设置
        masksView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: masksView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: masksView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: masksView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: reginButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        
    }
    //MARK: - 懒加载控件
    lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    
    lazy var homeIconViwe: UIImageView = {
        let homeIV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return homeIV
    }()
    
    lazy var textLable: UILabel = {
        let textLable = UILabel()
        textLable.text = "关注一些人,回这里看看有什么信息"
        textLable.textAlignment = NSTextAlignment.Center
        textLable.textColor = UIColor.darkGrayColor()
        textLable.font = UIFont.systemFontOfSize(14)
        textLable.numberOfLines = 0
        textLable.sizeToFit()

        return textLable
    }()
    
    
    lazy var masksView:UIImageView = {
        let maskV = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return maskV
    }()
    
    
    lazy var reginButton: UIButton = {
        let reginBtn = UIButton()
        reginBtn.setTitle("注册", forState: UIControlState.Normal)
    
        reginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        reginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        reginBtn.addTarget(self, action: "regin", forControlEvents: UIControlEvents.TouchUpInside)
        return reginBtn
        }()
    
    lazy var loginButton: UIButton = {
        let logBtn = UIButton()
        logBtn.setTitle("登录", forState: UIControlState.Normal)
        logBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        logBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        logBtn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return logBtn
        }()
}

